grammar SimpleC;
@header {        //一种action,定义生成的词法语法解析文件的头，当使用java的时候，生成的类需要包名，可以在这里统一定义
 package antlr.c;
 }
compilationUnit: functionDeclaration* EOF;

functionDeclaration: type ID '(' parameterList? ')' compoundStatement;

parameterList: parameter (',' parameter)*;

parameter: type ID;

compoundStatement: '{' statement* '}';

statement:
    variableDeclaration
    | returnStatement  // 添加这一行
    | assignment
    | functionCall
    | compoundStatement
    ;
returnStatement: 'return' expr ';';

variableDeclaration: type ID ';';

assignment: ID '=' expr ';';



expr
        : expr '+' term
        | expr '-' term
        | term
        ;

term
         : term '*' factor
         | term '/' factor
         | factor
         ;

factor
        : INT
        | ID
        | '(' expr ')'
        | functionCall
        ;


functionCall: ID '(' argumentList? ')' ';';

argumentList: expr (',' expr)*;

type: 'int';

ID: [a-zA-Z_][a-zA-Z_0-9]*;

INT: [0-9]+;

WS: [ \t\n\r]+ -> skip;
